package com.springboot.test.common.response;

import com.springboot.test.common.enums.ErrorCode;
import lombok.Data;

/**
 * 统一返回结果封装
 * @param <T>
 */
@Data
public class Response<T> {
    private Integer code;
    private String msg;
    private T data;

    private Response(){
        this.code = ErrorCode.SUCCESS.getCode();
        this.msg = ErrorCode.SUCCESS.getMessage();
    }
    private Response(T data) {
        this.code = ErrorCode.SUCCESS.getCode();
        this.msg = ErrorCode.SUCCESS.getMessage();
        this.data = data;
    }
    private Response(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }
    public static <T> Response<T> success(T data){
        return new Response<>(data);
    }
    public static <T> Response<T> error(Integer code, String msg) {

        return new Response<>(code, msg);
    }

    public static <T> Response<T> error(ErrorCode errorCode) {

        return new Response<>(errorCode.getCode(), errorCode.getMessage());
    }

    public static <T> Response<T> success(){
        return new Response<>();
    }

    public static <T> Response<T> error() {
        return error(ErrorCode.ERROR);
    }
}
